package com.zdy.im.user.service.impl;


import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.ResultEnum;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.util.PageUtils;
import com.sixeco.framework.util.excel.ExcelUtils;
import com.zdy.im.user.dao.ImRoomInfoDao;
import com.zdy.im.user.dto.request.ImRoomInfoRequestDto;
import com.zdy.im.user.dto.response.ImRoomInfoResponseDto;
import com.zdy.im.user.entity.ImRoomInfo;
import com.zdy.im.user.service.ImRoomInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.util.*;


/**
 * @ClassName: ImRoomInfoDao.java
 * @Description: 群信息表的数据交互处理类
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
 */
@Service
public class ImRoomInfoServiceImpl  implements ImRoomInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ImRoomInfoDao imRoomInfoDao;



    /**
     * 保存群信息表同时更新数据库和缓存的实现方法
     * @param imRoomInfoRequestDto ImRoomInfoResponseDto
     * @return
     */
    public ImRoomInfoResponseDto saveImRoomInfo(ImRoomInfoRequestDto imRoomInfoRequestDto){
        if(imRoomInfoRequestDto== null){
            return null;
        }
        ImRoomInfo imRoomInfo = ImRoomInfo.build().clone(imRoomInfoRequestDto);
        boolean result = imRoomInfoDao.save(imRoomInfo);
        if(!result){
            return null;
        }
        ImRoomInfoResponseDto imRoomInfoResponseDto = ImRoomInfoResponseDto.build().clone(imRoomInfo);
        return imRoomInfoResponseDto;


    }

    public boolean saveBatchImRoomInfo(Collection<ImRoomInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() !=  batchSize){
            return false;
        }
        List<ImRoomInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImRoomInfo.build().clone(e)));
        boolean result = imRoomInfoDao.saveBatch(list,batchSize);
        return result;
    }


    public boolean saveOrUpdateBatchImRoomInfo(Collection<ImRoomInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImRoomInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImRoomInfo.build().clone(e)));
        boolean result = imRoomInfoDao.saveOrUpdateBatch(list,batchSize);
        return result;
    }


    public boolean updateBatchById(Collection<ImRoomInfoRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImRoomInfo> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImRoomInfo.build().clone(e)));
        boolean result =  imRoomInfoDao.updateBatchById(list,batchSize);
        return result;
    }

    /**
     * 更新群信息表同时更新数据库和缓存的实现方法
     * @param imRoomInfoRequestDto  ImRoomInfoResponseDto
     * @return
     */
    public boolean updateImRoomInfo(ImRoomInfoRequestDto imRoomInfoRequestDto){

          if(null ==  imRoomInfoRequestDto){
              return false;
          }

        ImRoomInfo imRoomInfo = ImRoomInfo.build().clone(imRoomInfoRequestDto);

        return imRoomInfoDao.updateById(imRoomInfo);


    }







    /**
     * 通过主键ID删除对象信息实现缓存和数据库,同时删除的方法
     * @param  idList
     * @return
     */
    public int delImRoomInfoByIds(List<Long> idList){
        boolean result = false;
        if(null == idList){
            return ResultEnum.FAIL.id();
        }
        if( idList.size() == 1) {
            result = imRoomInfoDao.removeById(idList.get(0));
        }else {
            result =  imRoomInfoDao.removeByIds(idList);
        }
        if(result){
            return ResultEnum.SUCCESS.id();
        }
        return ResultEnum.FAIL.id();

    }


    /**
     * 通过主键ID更新对象群信息表实现缓存和数据库更新的方法
     * @param  imRoomInfoId
     * @return
     */
    public ImRoomInfoResponseDto getImRoomInfoById(long imRoomInfoId){
        if(imRoomInfoId < 0 ){
            return null;
        }
        ImRoomInfo imRoomInfo =  imRoomInfoDao.getById(imRoomInfoId);
        if(imRoomInfo == null){
            return null;
        }
        ImRoomInfoResponseDto imRoomInfoResponseDto = ImRoomInfoResponseDto.build().clone(imRoomInfo);

        return imRoomInfoResponseDto ;

    }

    @Override
    public List<ImRoomInfoResponseDto> getImRoomInfoByPage(BasePage page) {
        return null;
    }


    /**
    * 查询条件
    */
    public QueryWrapper<ImRoomInfo> checkQueryCondition(BasePage basePage,Collection<Long> ids) {
        QueryWrapper<ImRoomInfo> queryWrapper = new QueryWrapper<>();
        ImRoomInfoRequestDto imRoomInfoRequestDto = (ImRoomInfoRequestDto) basePage.getParamObject();
        if(!CollectionUtils.isEmpty(ids)){
            queryWrapper.in("id",ids);
        }
        if(StringUtils.isNotBlank(imRoomInfoRequestDto.getRoomName())){
            queryWrapper.like("room_name",imRoomInfoRequestDto.getRoomName());
        }
        queryWrapper.eq("status",1);
        return queryWrapper;
    }


    /**
     * 通过分页获取ImRoomInfo信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author suven
     * @date 2021-05-11 15:35:25
     */
    public List<ImRoomInfoResponseDto> getImRoomInfoByPage(
            ResponseResultList<ImRoomInfoResponseDto> responseResultList, BasePage basePage,Collection<Long> ids){


        List<ImRoomInfoResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        IPage<ImRoomInfo> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        QueryWrapper<ImRoomInfo> queryWrapper = checkQueryCondition(basePage, ids);

        IPage<ImRoomInfo> page = imRoomInfoDao.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImRoomInfo>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        list.forEach(imRoomInfo -> {
                ImRoomInfoResponseDto imRoomInfoResponseDto = ImRoomInfoResponseDto.build().clone(imRoomInfo);

            resDtoList.add(imRoomInfoResponseDto);
        });
        responseResultList.setTotal((int) page.getTotal());
        return resDtoList;


    }

    /**
     * 通过分页获取ImRoomInfo信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author suven
     * @date 2021-05-11 15:35:25
     */
    public ResponseResultList<ImRoomInfoResponseDto> getImRoomInfoByNextPage(BasePage basePage,Collection<Long> ids){
        ResponseResultList<ImRoomInfoResponseDto> responseResultList = ResponseResultList.build();
        List<ImRoomInfoResponseDto>  list = this.getImRoomInfoByPage(responseResultList,basePage,ids);
        if(null == list ){
            list = new ArrayList<>();
        }
        boolean isNext =  basePage.isNextPage(list);
        responseResultList.toIsNextPage(isNext).toList(list);
        return responseResultList;

    }

    public PageUtils queryPage(Map<String, Object> params) {
        IPage iPage =  new Query<ImRoomInfo>().getPage(params);
        QueryWrapper<ImRoomInfo> queryWrapper = new QueryWrapper<>();
        IPage<ImRoomInfo> page = imRoomInfoDao.page(iPage,queryWrapper);
        return new PageUtils(page);
    }


    /**
     * 保存群信息表同时更新数据库和缓存的实现方法
     * @return
     */
    public ImRoomInfo  setImRoomInfo(){
        ImRoomInfo imRoomInfo = new ImRoomInfo();
        /**
 			//imRoomInfo.setCreateDate (Date createDate);
 			//imRoomInfo.setModifyDate (Date modifyDate);
 			//imRoomInfo.setStatus (int status);
 			//imRoomInfo.setMasterId (long masterId);
 			//imRoomInfo.setCreateId (long createId);
 			//imRoomInfo.setRoomNumber (long roomNumber);
 			//imRoomInfo.setRoomName (String roomName);
 			//imRoomInfo.setRoomImg (String roomImg);
 			//imRoomInfo.setRoomNotice (String roomNotice);
 			//imRoomInfo.setRemarks (String remarks);
		**/

        return imRoomInfo;
    }

    public boolean saveData(InputStream initialStream) {
        return ExcelUtils.readExcel(initialStream,imRoomInfoDao, ImRoomInfo.class,0);
    }

    /**
     * 查询某个用户，所有好友列表
     * @param ids
     * @return
     */
    public List<ImRoomInfoResponseDto> selectListByIds(Collection<Long> ids){
        if(CollectionUtils.isEmpty(ids)){
            return null;
        }
        List<ImRoomInfo> list = imRoomInfoDao.selectListByIds( ids);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ImRoomInfoResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add(ImRoomInfoResponseDto.build().clone(e));
        });
        return lists;
    }

    @Override
    public Map<Long, ImRoomInfoResponseDto> getRoomInfoByIds(Collection<Long> idList) {
        if(null == idList){
            return null;
        }
        List<ImRoomInfo> rooms = imRoomInfoDao.getListByIds(idList);
        if(null == rooms || rooms.isEmpty()){
            return null;
        }
        Map<Long, ImRoomInfoResponseDto> map = new HashMap<>();
        rooms.forEach(e->{
            map.put(e.getId(),ImRoomInfoResponseDto.build().clone(e));
        });
        return map;
    }
}
